#include "EEPROM.h"


static BSP_IIC_Typedef_t E2PROM_Config = {
        .port = GPIOB,
        .SDA_Pin = GPIO_Pin_11,
        .SCK_Pin = GPIO_Pin_10,
        .GPIO_Speed = GPIO_Speed_50MHz,
        .GPIO_Mode = GPIO_Mode_AF_OD,

        .RCC_APB1Periph = RCC_APB1Periph_I2C2,
        .RCC_APB2Periph = RCC_APB2Periph_GPIOB,

        .I2Cx = I2C2,
        .I2C_ClockSpeed = 100000,
        .I2C_Mode = I2C_Mode_I2C,
        .I2C_DutyCycle = I2C_DutyCycle_2,
        .I2C_OwnAddress1 = 0xA0,
        .I2C_Ack = I2C_Ack_Enable,
        .I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit,

        .RCC_APB1PeriphClockCmd = RCC_APB1PeriphClockCmd,
        .RCC_APB2PeriphClockCmd = RCC_APB2PeriphClockCmd
};



E2PROM::E2PROM() : dev(&E2PROM_Config){

}


E2PROM::~E2PROM() {

}


void E2PROM::WriteOneByte(u16 WriteAddr, u8 DataToWrite) {
    while(I2C_GetFlagStatus(I2C2, I2C_FLAG_BUSY) != RESET)
            ;
        I2C_GenerateSTART(I2C2, ENABLE);

        while(!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_MODE_SELECT))
            ;
        I2C_Send7bitAddress(I2C2, 0XA0, I2C_Direction_Transmitter);

        while(!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED))
            ;

    #if(Address_Lenth == Address_8bit)
        I2C_SendData(I2C2, (u8)(WriteAddr & 0x00FF));
        while(!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_BYTE_TRANSMITTED))
            ;

    #elif(Address_Lenth == Address_16bit)
        I2C_SendData(I2C2, (u8)(WriteAddr >> 8));
        while(!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_BYTE_TRANSMITTED))
            ;

        I2C_SendData(I2C2, (u8)(WriteAddr & 0x00FF));
        while(!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_BYTE_TRANSMITTED))
            ;

    #endif

        if(I2C_GetFlagStatus(I2C2, I2C_FLAG_TXE) != RESET)
        {
            I2C_SendData(I2C2, DataToWrite);
        }

        while(!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_BYTE_TRANSMITTED))
            ;
        I2C_GenerateSTOP(I2C2, ENABLE);
}

u8 E2PROM::ReadOneByte(u16 ReadAddr) {
    u8 temp = 0;

    while(I2C_GetFlagStatus(I2C2, I2C_FLAG_BUSY) != RESET)
        ;
    I2C_GenerateSTART(I2C2, ENABLE);

    while(!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_MODE_SELECT))
        ;
    I2C_Send7bitAddress(I2C2, 0XA0, I2C_Direction_Transmitter);

    while(!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED))
        ;

#if(Address_Lenth == Address_8bit)
    I2C_SendData(I2C2, (u8)(ReadAddr & 0x00FF));
    while(!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_BYTE_TRANSMITTED))
        ;

#elif(Address_Lenth == Address_16bit)
    I2C_SendData(I2C2, (u8)(ReadAddr >> 8));
    while(!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_BYTE_TRANSMITTED))
        ;

    I2C_SendData(I2C2, (u8)(ReadAddr & 0x00FF));
    while(!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_BYTE_TRANSMITTED))
        ;

#endif

    I2C_GenerateSTART(I2C2, ENABLE);

    while(!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_MODE_SELECT))
        ;
    I2C_Send7bitAddress(I2C2, 0XA0, I2C_Direction_Receiver);

    while(!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED))
        ;
    while(I2C_GetFlagStatus(I2C2, I2C_FLAG_RXNE) == RESET)
        I2C_AcknowledgeConfig(I2C2, DISABLE);

    temp = I2C_ReceiveData(I2C2);
    I2C_GenerateSTOP(I2C2, ENABLE);

    return temp;
}


void E2PROM::Write(u16 WriteAddr, u8 *pBuffer, u16 NumToWrite) {
    while(NumToWrite--)
    {
        WriteOneByte(WriteAddr, *pBuffer);
        WriteAddr++;
        pBuffer++;
//        Delay_Ms(2);
        My_Delay_Us(2000);
    }
}

void E2PROM::Read(u16 ReadAddr, u8 *pBuffer, u16 NumToRead) {
    while(NumToRead)
    {
        *pBuffer++ = ReadOneByte(ReadAddr++);
        NumToRead--;
    }
}


